<?php
/**
 * Copyright (c) Sandeep Manne (sandeep.manne@gmail.com) 
 *
 * @author  Sandeep Manne
 * @since   23-May-2009, 1:13:05 PM
 * @version $
 */

/**
 * @see Zend_Form
 */
require_once 'Zend/Form.php';

class Core_Form extends Zend_Form
{
    /**
     * Removes the default decorator that surrounds the form elements.
     *
     * @param array|Zend_Config $options
     * @return void
     */
    public function __construct($options = null)
    {
        parent::__construct();
        $this->removeDecorator('HtmlTag');
    }

    /**
     * Create an element
     *
     * Acts as a factory for creating elements. Elements created with this
     * method will not be attached to the form, but will contain element
     * settings as specified in the form object (including plugin loader
     * prefix paths, default decorators, etc.).
     *
     * @param  string $type
     * @param  string $name
     * @param  array|Zend_Config $options
     * @return Zend_Form_Element
     */
    public function createElement($type, $name, $options = null)
    {
        $element = parent::createElement($type, $name, $options);

        if ($type != 'file') {
            $viewHelper = 'ViewHelper';
        } else {
            $viewHelper = 'File';
        }

        $element->setDecorators(array(
        	$viewHelper,
        	'Errors',
            array(
            	'Description',
                array(
                	'tag' => 'div',
                	'class' => 'form-description'
                )
            ),
            array(
            	'HtmlTag',
                array(
                	'tag' => 'div',
            		'class' => 'form-field'
                )
            ),
            array(
                array('labelClose' => 'HtmlTag'),
                array(
                	'tag' => 'div',
                	'closeOnly' => true,
                	'placement' => 'prepend')
            ),
            'Label',
            array(
                array('labelOpen' => 'HtmlTag'),
                array(
                	'tag' => 'div',
                	'openOnly' => true,
                	'placement' => 'prepend',
                	'class' => 'form-label')
            )
        ));

        return $element;
    }

    /**
     * Add a display group
     *
     * Groups named elements for display purposes.
     *
     * If a referenced element does not yet exist in the form, it is omitted.
     *
     * @param  array $elements
     * @param  string $name
     * @param  array|Zend_Config $options
     * @return Core_Form
     * @throws Zend_Form_Exception if no valid elements provided
     */
    public function addDisplayGroup(array $elements, $name, $options = null)
    {
        parent::addDisplayGroup($elements, $name, $options);

        $this->setDisplayGroupDecorators(array(
            'FormElements',
            array(
            	'HtmlTag',
                array('tag' => 'div', 'class' => 'form-fieldset-wrapper')
            ),
            'Fieldset'
        ));

        return $this;
    }

    /**
     * Creates an input of type image.
     *
     * @param string $buttonName
     * @param string $imagePath
     * @return Zend_Form_Element_Interface
     */
    public function createImageButton($buttonName, $imagePath)
    {
        return $this->createElement('image', $buttonName)
                    ->setImage($imagePath)
                    ->removeDecorator('Label')
                    ->removeDecorator('LabelOpen')
                    ->removeDecorator('LabelClose');
    }
    
	/**
     * Creates an input of type image.
     *
     * @param string $buttonName
     * @param string $imagePath
     * @return Zend_Form_Element_Interface
     */
    public function createButton($buttonName)
    {
        return $this->createElement('submit', $buttonName)
                    ->removeDecorator('Label')
                    ->removeDecorator('LabelOpen')
                    ->removeDecorator('LabelClose');
    }

    /**
     * Insert a div element to break in a new line.
     *
     * @param string $breakClass
     * @return Zend_Form_Element_Interface
     */
    public function insertBreak($breakClass = null)
    {
        return $this->createTag('line-break' . md5(microtime()), 'div', null, $breakClass);
    }

    /**
     * Returns an tag element.
     *
     * @param string $name
     * @param string $type
     * @param string $id
     * @param string $class
     * @return Zend_Form_Element
     */
    public function createTag($name, $type, $id = null, $class = null)
    {
        /**
         * @see Zend_Form_Element
         */
        require_once 'Zend/Form/Element.php';
        $element = new Zend_Form_Element(
            $name,
            array(
                'disableLoadDefaultDecorators' => true,
                'decorators' => array($type => 'HtmlTag')
            )
        );

        if (!empty($id)) {
            $element->getDecorator('HtmlTag')->setOption('id', $id);
        }

        if (!empty($class)) {
            $element->getDecorator('HtmlTag')->setOption('class', $class);
        }

        return $element;
    }
    
    /**
     * Used to populate options for form select element.
     *
     * @param $options array
     * @param $element string
     * @return unknown
     */
    public function populateSelect(array $options, $element)
    {
        $element = $this->getElement($element);
        foreach ($options as $key => $value) {
            $element->addMultiOption($key,$value);
        }
        return $this;
    }
    
    /**
     * Used to remove form elements.
     *
     * @param $elements array
     * @return unknown
     */
    public function removeElements(array $elements)
    {
        foreach($elements as $element) {
            $this->removeElement($element);
        }
        
        return $this;
    }
}
